package com.ddd.mill.application.product;


import com.ddd.mill.api.product.dto.request.AddProductDto;
import com.ddd.mill.api.product.dto.request.SaveProductByAdminDto;
import com.ddd.mill.api.product.dto.response.UpdateStatusDto;
import com.ddd.mill.domain.aggregate.product.ProductAggregate;
import com.ddd.mill.domain.aggregate.product.entity.Product;
import com.ddd.mill.domain.aggregate.product.repository.ProductRepository;
import com.ddd.mill.domain.aggregate.sku.ProductSkuAggregate;
import com.ddd.mill.infrastructure.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author liu
 * @title: ProductService
 * @projectName trading-center
 * @date 2020/11/3010:56
 */
@Slf4j
@Service
public class ProductApplicationService {

    @Resource
    ProductAggregate productAggregate;
    @Resource
    ProductSkuAggregate productSkuAggregate;
    @Resource
    ProductRepository productRepository;


    public void saveProductByAdmin(SaveProductByAdminDto dto, Long adminId){
        //保存商品信息
        productAggregate.saveProduct(dto, adminId);
        //todo 保存sku


    }

    public void updateProduct(Long id, AddProductDto request, Long adminId){

    }

    public void updateStatus(Long id, UpdateStatusDto request, Long adminId){
        productAggregate.updateStatus(id, request, adminId);
    }

    public void deleteProduct(String[] idList, Long adminId){
        productAggregate.deleteProduct(idList,adminId);
    }

    public void copyProduct(String[] split, Long adminId){
        try {
            for (String s : split) {
                Long id = Long.parseLong(s);
                Product product = productRepository.load(id);
                //复制商品信息
                productAggregate.copyProduct(adminId,product);
                //复制sku
                productSkuAggregate.copyProductSku(adminId,product);
            }
        }catch (Exception e){
            throw new BizException("复制失败");
        }
    }

}
